###############################################################
###############################################################
#### Stefan Müller, Samuel Brazys, and Alexander Dukalskis
#### Replication Scripts for: 
#### Discourse Wars and 'Mask Diplomacy': China's Global Image Management in Times of Crisis 
#### Political Research Exchange, 2024
#### Link to paper: https://doi.org/10.1080/2474736X.2024.2309178
###############################################################
###############################################################

## Note: check the 000_README.pdf file on Harvard Dataverse for 
## the full replication instructions and information on all code scripts.
## Link to Dataverse repository: https://doi.org/10.7910/DVN/KRXMXJ
## Please contact the authors if you have any questions or suggestions. 
## Note: due to copyright restrictions some of the files cannot be shared publicly.
## However, we provide all replication scripts and intermediate objects to reproduce
## the plots and tables included in the paper and Supporting Information.

## This file visualises the LSS polarity scores. 

# load packages
library(dplyr)
library(ggplot2)
library(ggrepel)
library(rio)


# load custom ggplot2 scheme
source("function_theme_base.R")

# get polarity scores
dat_neg_pos <- read.csv("data_scores_lss_words.csv")


# check scores for China and media words
dat_words <- rio::import("data_dictionary_china_positive.xlsx")

# get terms from Whitepaper
dat_words_whitepaper <- rio::import("data_topfeatures_fighting_covid.xlsx") %>% 
    filter(feature %in% c(
        "prevention",
        "help",
        "fight",
        "commitment",
        "coordination",
        "assistance",
        "support",
        "solidarity"))


words_china <- dat_words$word

words_whitepaper <- dat_words_whitepaper$feature

# indicate terms included in whitepaper/china words
dat_neg_pos <- dat_neg_pos %>% 
    mutate(whitepaper_word = ifelse(word %in% words_whitepaper, TRUE, FALSE)) %>% 
    mutate(china_word = ifelse(word %in% words_china, TRUE, FALSE))


# only filter terms with at least 300 mentions
dat_neg_pos_subset <- dat_neg_pos %>% 
    filter(freq > 300)

# set seed for reproducibility of word positions
set.seed(64)
ggplot(dat_neg_pos_subset, aes(x = beta, y = log(freq))) +
    geom_text(aes(label = word), colour = "grey90") +
    geom_point(data =
                   filter(dat_neg_pos_subset, whitepaper_word == TRUE),
               size = 3) +
    ggrepel::geom_label_repel(data =
                                  filter(dat_neg_pos, whitepaper_word == TRUE),
                              aes(x = beta, y = log(freq),
                                  label = word),
                              max.overlaps= 30,
                              size = 5) +
    scale_fill_manual(values = c("grey50", "black")) +
    labs(x = "LSS Word Scores",
         y = "Word Frequency (Log)")
# store in various file formats
ggsave("fig_07.pdf",
       width = 9, height = 5)
ggsave("fig_07.png",
       dpi = 300,
       width = 9, height = 5)
ggsave("fig_07.eps",
       device = "eps", width = 9, height = 5)
